package com.wilson.integration.lease.admin.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilson.integration.lease.admin.service.SystemUserService;
import com.wilson.integration.lease.common.entity.SystemUser;
import com.wilson.integration.lease.common.entity.vo.system.user.SystemUserItemVO;
import com.wilson.integration.lease.common.entity.vo.system.user.SystemUserQueryVO;
import com.wilson.integration.lease.common.enums.BaseStatus;
import com.wilson.integration.lease.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Resource
    private SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVO>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVO queryVO) {
        IPage<SystemUser> page = new Page<>(current, size);
        return Result.ok(systemUserService.pageSystemUserByQuery(page, queryVO));
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVO> getById(@RequestParam Long id) {
        return Result.ok(systemUserService.getSystemUserById(id));
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        if(ObjectUtil.isNotNull(systemUser.getPassword())){
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
        }
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        long count = systemUserService.count(
                new LambdaQueryWrapper<SystemUser>()
                        .eq(SystemUser::getUsername, username));
        return Result.ok(count == 0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        systemUserService.update(
                new LambdaUpdateWrapper<SystemUser>()
                        .eq(SystemUser::getId, id)
                        .set(SystemUser::getStatus, status));
        return Result.ok();
    }
}
